package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.query.DailyQuery;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.SevenUploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;
    @Autowired
    private IRegionService regionService;
    //待发布状态的游记管理
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")DailyQuery qo, Model model){
        //设置查询条件,查询的是审核的游记
        PageInfo pageInfo = dailyService.query(qo);
        model.addAttribute("regions",regionService.list());
        model.addAttribute("result", pageInfo);

        return "daily/list";
    }
    //保存或者修改推荐游记
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Daily daily, MultipartFile file){
        JSONResult jsonResult = new JSONResult();
        try {
            //保存照片
            if (file !=null && file.getSize()>0){
                String uploadQiniu = SevenUploadUtil.uploadQiniu(file);
                /*String filePath = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PATH+"/upload/");*/
                daily.setCoverUrl(uploadQiniu);
            }
            dailyService.saveOrUpdate(daily);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    //保存或者修改推荐游记
    @RequestMapping("/getContentBydailyId")
    @ResponseBody
    public Object getContentBydailyId(Long dailyId){
        return dailyService.getContentBydailyId(dailyId);
    }

}
